﻿using System;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.Utilities;
using StructureMap;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Messages;

namespace MedicalCare.Admin
{
    public partial class EquipmentTypeList : System.Web.UI.Page
    {
        private IEquipmentTypeAdaptor _etAdaptor;
        private int equipmentTypeId { get { return Convert.ToInt32(ViewState["equipmentTypeId"]); } set { ViewState["equipmentTypeId"] = value; } }
        private string action { get { return Convert.ToString(ViewState["action"]); } set { ViewState["action"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEquipmentTypeList();
            }
        }

        private void BindEquipmentTypeList()
        {
            _etAdaptor = ObjectFactory.GetInstance<IEquipmentTypeAdaptor>();
            var EquipmentTypeList = _etAdaptor.GetAllEquipmentType();
            if (EquipmentTypeList != null)
            {
                dlEquipmentType.DataSource = EquipmentTypeList;
                dlEquipmentType.DataBind();
            }
            
        }

        private EquipmentTypeMessage GetEquipmentTypeMessage(string action)
        {
            EquipmentTypeMessage equipmentTypeMessage = null;

            if (ActionType.DELETE.ToString().Equals(action))
            {
                equipmentTypeMessage = new EquipmentTypeMessage()
                {
                    Id=equipmentTypeId,
                    ModifyUser = AppPage.CurrentUser.UserName,
                    Valid = "N" 
                };
            }
            return equipmentTypeMessage;
        }

        protected void btnAddNewEquipmentType_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Admin/EquipmentTypeDetail.aspx?action=" + ActionType.NEW.ToString());
        }


        protected void dlEquipmentType_ItemCommand(object source, DataListCommandEventArgs e)
        {
            _etAdaptor = ObjectFactory.GetInstance<IEquipmentTypeAdaptor>();
            try
            {
                equipmentTypeId = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName.ToLower())
                {
                    case "del":
                        EquipmentTypeMessage equipmentTypeMessage= GetEquipmentTypeMessage(ActionType.DELETE.ToString());
                        int result = _etAdaptor.DeleteEquipmentTypeById(equipmentTypeMessage);
                        BindEquipmentTypeList();
                        break;
                    case "edit":
                        Server.Transfer("~/Admin/EquipmentTypeDetail.aspx?action=" + ActionType.EDIT.ToString() + "&Id=" + equipmentTypeId.ToString());
                        break;
                }
            }
            catch
            {

            }
        }
    }
}